﻿package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.AntiAliasType;
	import flash.text.engine.JustificationStyle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	import flash.utils.setInterval;
	import flash.utils.setTimeout;
	import samples.BaseSample;
	import samples.d1.*;
	import samples.d2.curves.Curves_0;
	import samples.d2.lines.*;
	import samples.d2.parallels.*;
	import samples.d2.parallels.trames.*;
	import samples.d2.shapes.Arc0;
	import samples.d2.trames.*;
	import shapes.shapes2D.*;
	import shapes.shapes2D.distribution.modulo.Modulo;
	import shapes.shapes2D.distribution.random.PRNG;
	import shapes.shapes2D.distribution.random.Random;
	import shapes.shapes2D.distribution.trigo.Pythagora;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class Lines extends Sprite 
	{
		
		private var bmp:Bitmap;
		private var bd:BitmapData;
		private var sample:BaseSample;
		
		private var sampleId:int = 0;
		private var operation:int = 0;
		private var tf:TextFormat;
		private var txt:TextField;
		
		public function Lines():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			_stage = stage;
			
			_stage.align = StageAlign.TOP_LEFT;
			_stage.scaleMode = StageScaleMode.NO_SCALE;
			_stage.displayState = StageDisplayState.FULL_SCREEN;
			
			_stage.addEventListener( KeyboardEvent.KEY_DOWN, keyHandler )
			
			txt = new TextField();
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.antiAliasType = AntiAliasType.ADVANCED;
			addChild( txt );
			
			tf = new TextFormat( 'Verdana', 16, 0xFFFFFF );
			sampleId = 0;
			
			keyHandler();
			
		}
		
		private function keyHandler(e:KeyboardEvent = null ):void 
		{
			if (e != null  
			&&	e.keyCode != Keyboard.RIGHT 
			&& 	e.keyCode != Keyboard.LEFT 
			&& 	e.keyCode != Keyboard.UP ) return;
			
			if (e != null )
			{
				switch( e.keyCode )
				{
					case Keyboard.RIGHT:
					sampleId++;
					break;
					
					case Keyboard.LEFT:
					sampleId--;
					break;
					
				}
			}
			//if ( sampleId > 38 ) sampleId = 38;
			if ( sampleId < 0) sampleId = 0;
			var n:int = numChildren;
			
			while ( n-- )
			{
				if( !( getChildAt( n ) is TextField ) ) removeChildAt( n );
			}
			
			var description:String = '';
			var op:int = 0;
			switch( sampleId )
			{
				
				//lignes rendu
				case op++:
					description = 'drawing a line\nkey notions: \n- normalization : mapping arbitrary values between 0 & 1\n- interpolation: varying a value between 2 values\n- center of mass between 2 points aka the "barycenter"';
					sample = new Sample2(); // trace une ligne
				break;
				
				case op++:
					description = 'stroke weight:\n indexing the value of the stroke on the barycenter';
					sample = new Sample3(); // graisse 
				break;
				
				
				//modulo 
				
				case op++:
					description = 'exercise created by Petri Leskinen aka PIXELERO and available here:\nhttp://pixelero.wordpress.com/2008/07/28/various-functions-with-modulo/\nit describes various ways to create patterns with th e help of modulos and very simple operations';
					addChild( new Modulo() );
					txt.text = sampleId.toString() +' : ' + description;
					txt.setTextFormat( tf );
					return;
				break;
				
				case op++:
					description = 'the random methods can also distribute values in one dimension\nagain an article by Petri Leskinen aka Pixelero\nhttp://pixelero.wordpress.com/2008/04/24/various-functions-and-various-distributions-with-mathrandom/';
					addChild( new Random() );
					txt.text = sampleId.toString() +' : ' + description;
					txt.setTextFormat( tf );
					return;
				break;
				
				
				
				
				//case op++:
					//description = 'varying the stroke weight & alpha & the starting & ending values of the line';
					//sample = new Sample4();	// graisse alpha
				//break;
				
				case op++:
					description = 'regular dashed line';
					sample = new Sample50();// pointille 
				break;
				
				case op++:
					description = 'random dashed line round stroke';
					sample = new Sample5(); // discontinuite rond
				break;
				
				//case op++:
					//description = 'random dashed line square stroke';
					//sample = new Sample51();// discontinuite carre
				//break;
				
				case op++:
					description = 'variable stroke weight + alpha round';
					sample = new Sample6(); // graisse variable + alpha + rond
				break;
				
				case op++:
					description = 'variable stroke weight + alpha square';
					sample = new Sample60();// graisse variable + alpha + carre
				break;
				
				
				
				//paralleles
				case op++:
					description = '2 parallel lines';
					sample = new Parallel0(); 	// 2 lignes paralleles
				break;
				
				case op++:
					description = 'OMFG we have 2D !!! ';
					sample = new Parallel01(); 	// 2D !
				break;
				
				case op++:
					description = '50 parallel lines';
					sample = new Parallel1();	// 50 lignes
				break;
				
				case op++:
					description = '50 lines with variables length';
					sample = new Parallel2();	// 50 lignes longueurs variables
				break;
				
				//case op++:
					//description = '50 lines with variables length & stroke weight';
					//sample = new Parallel3();	// 50 lignes longueurs et graisses 
				//break;
			
				//case op++:
					//description = '50 lines with variables length & round stroke weight animated';
					//sample = new Parallel30();	// 50 lignes longueurs et graisses rond animation
				//break;
				
				case op++:
					description = '50 lines with variables length & square stroke weight animated #1';
					sample = new Parallel31();	// 50 lignes longueurs et graisses carre animation
				break;
				
				case op++:
					description = '50 lines with variables length & square stroke weight animated #2';
					sample = new Parallel4();	// 50 lignes longueurs et graisses carre animation variante
				break;
				
				case op++:
					description = 'regular grid (weft, screen, frame)';
					sample = new Trame0(); 	//grille
				break;
				
				case op++:
					description = 'distribution and stroke variations';
					sample = new Trame1();	//variation graisses
				break;
				
				//case op++:
					//description = 'unbalanced grid (weft, screen, frame)';
					//sample = new Trame2();	//trame desaxee
				//break;
				
				case op++:
					description = 'composition';
					sample = new Trame5();	//compo grille graisses longueurs variables
				break;
				
				//case op++:
					//description = 'animated unbalanced grid';
					//sample = new Trame3();	//grille désaxee animation
				//break;
				case op++:
					description = 'mobile parallel lines are boring';
					sample = new _Line0(); 	// lignes paralleles mobiles
				break;
			
				case op++:
					description = 'animated unbalanced grid weights';
					sample = new Trame4();	//grille désaxee animation + graisses
				break;
				
				
				
				
				//lines
				
				
				case op++:
					description = 'mobile lines';
					sample = new _Line1();	// lignes aléatoires + bounding box
				break;
			
				//
				
				case op++:
					description = 'point set + random lines';
					sample = new Lines1();	// set de points + lignes aléatoires
				break;
				
				case op++:
					description = 'point set + polyLine lineTo';
					sample = new Lines2();	// set de points + polyline	lineTo
				break;
				
				case op++:
					description = 'point set + polyLine curveTo';
					sample = new Lines3();	// set de points + polyline curveTo
				break;
				
				case op++:
					description = 'point set + a nicer curveTo by Philippe Elsass';
					sample = new Lines4();// -> smooth curves
				break;
				
				//arc
				
				case op++:
					description = 'arcs also are lines';
					sample = new Arc0();
				break;
				
				//curves
				
				case op++:
					description = 'splines';
					sample = new Curves_0();// -> smooth curves
				break;
				
					
				//+ recursion
				
				case op++:
					description = 'iteration / recursion';
					sample = new Recursion();
				break;
				
				case op++:
					description = 'WOOOOO our first FX!';
					sample = new Lines5();//electric
				break;
				
				case op++:
					description = 'circular origins circular destinations';
					sample = new Lines6();//hair ball
					operation++;
				break;
				
				
				//distance functions
				
				
				case op++:
					description = 'node garden';
					sample = new Lines7();//node garden
				break;
				
				case op++:
					description = 'we can also draw a vectorized shape';
					sample = new Lines9();//vectorized shape
				break;
				
				case op++:
					description = 'the distance check is the base for the scribbler';
					sample = new Lines8();//scribbler
				break;
				
				
				
			}
			
			txt.text = sampleId.toString() +' : ' + description;
			txt.setTextFormat( tf );
			if( sample != null ) addChild( sample );
			
		}
		
	}
	
}